//
// Created by v on 19-5-23.
//

#include "OpenGLQtEasy.h"
#include "cm_logprint.h"
#include <QtGui/qopenglfunctions.h>
#include <QtOpenGL/qglfunctions.h>
#include <QtOpenGL/qgl.h>

OpenGLQtEasy::OpenGLQtEasy(int width, int height, QWidget *parent)
    : QOpenGLWidget(parent) {
    m_width = width;
    m_height = height;
    setGeometry(0, 0, m_width, m_height);
}

OpenGLQtEasy::~OpenGLQtEasy() {

}

void OpenGLQtEasy::initializeGL() {
    LOG(" init \n");
    initializeOpenGLFunctions();
    glClearColor(1.0, 1.0, 1.0, 1.0);
    glClearDepthf(1.0);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_TEXTURE_2D);
}

void OpenGLQtEasy::paintGL() {
//    LOG("\n");
    QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();

    glDisable(GL_DEPTH_TEST);

    QPainter painter(this);

    painter.drawImage(this->rect(), m_image);
    glEnable(GL_DEPTH_TEST);

}

void OpenGLQtEasy::resizeGL(int width, int height) {
    LOG("[%d,%d]\n", width, height);
    this->resize(width, height);
    glViewport( 0, 0, (GLint)width, (GLint)height );
}

void OpenGLQtEasy::renderDisplay(const QImage &image) {
    m_image = image;
//    LOG("------------- render size [%d, %d]\n", m_image.rect().size().width(), m_image.height());
    update();
}
